package org.example.task;

import javafx.collections.ObservableList;
import org.example.Device;
import org.example.appstart.StartAppInfo;
import org.example.utlis.CmdUtils;
import org.example.utlis.FXUtils;

import java.util.ArrayList;
import java.util.Arrays;

public class Task {
    private String deviceId;
    private Type type;
    private Object[] args;
    public enum Type {
        YLOG_PAGE,
        YLOG_PULL,
        JIT,
        PACKAGE_NAME,
        APK_PULL,
        SCREEN_CAP_AND_PULL,
        SCREEN_RECORD_AND_PULL,
        TRACE_PERFETTO,
        APP_START,
        DUMP_PACKAGE
    }

    private Task(String deviceId, Type type, Object... args) {
        this.deviceId = deviceId;
        this.type = type;
        this.args = args;
    }

    public static Task build(String deviceId, Type type, Object... args) {
        return new Task(deviceId, type, args);
    }

    public String run() {
        String result = "";
        if (type == null) {
            return result;
        }
        CmdUtils.getInstance().onExeCmdListener.onSucceed("Run task: " + this);
        switch (type) {
            case YLOG_PAGE:
                CmdUtils.getInstance().exeStartYlog(deviceId);
                break;
            case YLOG_PULL:
                if (args != null && args.length == 1 && args[0] instanceof String) {
                    String savePath = (String)args[0];
                    CmdUtils.getInstance().exePullYlogAsync(deviceId, savePath);
                }
                break;
            case JIT:
                if (args != null && args.length == 3) {
                    String pkgName = (String) args[0];
                    int times = (Integer) args[1];
                    int mode = (Integer) args[2];
                    CmdUtils.getInstance().exeJitAsync(deviceId, pkgName, times, mode);
                }
                break;
            case PACKAGE_NAME:
                result = CmdUtils.getInstance().getPkgNameCurrentFocus(deviceId);
                break;
            case APK_PULL:
                if (args != null && args.length == 1 && args[0] instanceof String) {
                    CmdUtils.getInstance().exeGetApkAsync(deviceId, (String) args[0]);
                }
                break;
            case SCREEN_CAP_AND_PULL:
                if (args != null && args.length == 1 && args[0] instanceof String) {
                    CmdUtils.getInstance().exeScreenCap(deviceId);
                    CmdUtils.getInstance().exePullScreenCap(deviceId, (String) args[0]);
                }
                break;
            case SCREEN_RECORD_AND_PULL:
                if (args != null && args.length == 1 && args[0] instanceof String) {
                    //开始录屏
                    CmdUtils.getInstance().exeScreenRecordAsync(deviceId);
                    //弹窗提醒
                    FXUtils.initSimpleDialog("录屏: " + deviceId, deviceId + "录屏中……点击确定按钮停止录屏", () -> {
                        //这里停止录屏并pull，然后打开保存目录
                        CmdUtils.getInstance().exeStopScreenRecord(deviceId);
                        CmdUtils.getInstance().exePullScreenRecord(deviceId, (String) args[0]);
                    }, null);
                }
                break;
            case TRACE_PERFETTO:
                if (args != null && args.length == 4) {
                    int duration = (int) args[0];
                    String savePath = (String) args[1];
                    int times = (int) args[2];
                    String tags = (String) args[3];
                    CmdUtils.getInstance().exeTracePerfettoAndPullAsync(deviceId, duration, savePath, times, tags);
                }
                break;
            case APP_START:
                if (args != null && args.length == 5) {
                    //startAppInfoList, isColdStart, isBack, isRemove, times
                    ObservableList<StartAppInfo> startAppInfoList = (ObservableList<StartAppInfo>) args[0];
                    boolean isColdStart = (boolean) args[1];
                    boolean isBack = (boolean) args[2];
                    boolean isRemove = (boolean) args[3];
                    int times = (int) args[4];
                    CmdUtils.getInstance().exeAppStartAsync(deviceId, startAppInfoList, isColdStart, isBack, isRemove, times);
                }
                break;
            case DUMP_PACKAGE:
                if (args != null && args.length == 1 && args[0] instanceof String) {
                    CmdUtils.getInstance().exeDumpPkgInfo(deviceId, (String) args[0]);
                }
            default:
                break;
        }
        return result;
    }

    @Override
    public String toString() {
        return "Task{" +
                "deviceId='" + deviceId + '\'' +
                ", taskType=" + type +
                ", args=" + Arrays.toString(args) +
                '}';
    }
}
